$(function () {
    // 渲染表格数据 
    var form = layui.form;
    var layer = layui.layer;
    tableInfo();
    function tableInfo() {
        $.ajax({
            type: "get",
            url: "/my/article/cates",
            success: function (response) {
                var htmlStr = template('table-tmp', response);
                console.log(response);
                $('tbody').html(htmlStr);
            }
        });
    }


    // 添加绑定事件
    var index = null;
    $('#addType').on('click', function () {
        index = layer.open({
            type: 1,
            titile: '添加文章类别',
            area: ['500px', '250px'],
            content: $('#dialog-add').html()
        })

    })


    // 绑定提交事假
    $('body').on('submit', '#form-add', function (e) {
        e.preventDefault();
        $.ajax({
            type: "post",
            url: "/my/article/addcates",
            // serialize快速拿到表单中的值;必须是form表单，必须要有submit属性
            data: $(this).serialize(),
            success: function (response) {
                if (response.status != 0) {
                    layer.msg('添加类别失败')
                }
                tableInfo();
                layer.msg('添加类别成功');
                layer.close(index);
            }
        });
    })



    // 修改编辑
    $('tbody').on('click', '#exid', function () {
        index = layer.open({
            type: 1,
            titile: '修改文章类别',
            area: ['500px', '250px'],
            content: $('#dialog-exid').html()
        })
        //发送请求
        var id = $(this).attr('data-exid');
        $.ajax({
            type: "get",
            url: "/my/article/cates/" + id,
            success: function (response) {
                // form.val是layui的方法
                form.val('form-exid', response.data)
            }
        });
    })


    // 事件代理
    $('body').on('submit', '#form-exid', function (e) {
        e.preventDefault();
        $.ajax({
            type: "post",
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: function (response) {
                if (response.status != 0) {
                    return layer.msg('更新分类失败！')

                }
                layer.msg('更新分类成功！');
                layer.close(index);
                tableInfo();
            }
        });
    })



    // 删除
    $('tbody').on('click', '.btn-del', function () {
        var id = $(this).attr('data-id');
        layer.confirm('确认删除?', { icon: 3, title: '提示' }, function (index) {
            //do something
            $.ajax({
                type: "get",
                url: "/my/article/deletecate/" + id,
                data: $(this).serialize(),
                success: function (response) {
                    if (response.status != 0) {
                        return layer.msg('删除数据失败!');
                    }
                    layer.msg('删除数据成功!');
                    layer.close(index);
                    tableInfo();

                }
            })

        })

    })

})